<?php
if(!defined('IN_ORA')) {
	exit('Access Denied');
}
$GLOBALS['__page_cache__'] = true;
$act = $GLOBALS['act'] ==""?"index":$GLOBALS['act'];
$GLOBALS['tpl']->assign('act',$act);
$pid= intval($GLOBALS['_param']['p']);
$pid= $pid>0?$pid:1;
/*** 搜索 ***/
if($act == "v"){
	$key = clear_html($GLOBALS['_param']['key']);
	$key = preg_replace('/\'|\"|\=|update|select|delete|truncate|insert|drop|exec|eval|\$|\{|\}|\(|\)/i','',$key);
	if($key==""){
		redirect("index-index");
	}
	import("lib.page");
	if(!$GLOBALS['tpl']->isCached("search-v-key-$key-p-$pid")){
		/** 站点信息 **/
		$GLOBALS['tpl']->assign('sinfo',$GLOBALS['db']->getOne('select * from '.tn('info').' order by Id asc limit 1'));

		/** 导航信息 **/
		$GLOBALS['tpl']->assign('navs',$GLOBALS['db']->getAll('select * from '.tn('nav').' order by nav_sort asc limit 6'));

		/** 产品信息 **/
		import("lib.cat");
		$cates = cat_lib::getSubChild(0,2,' and cat_type=1');
		$GLOBALS['tpl']->assign('cates',$cates);
		/** 当前位置 **/
		$local = array();
		$local[] = array('url'=>get_url(true),'text'=>'产品搜索_'.$key);
		$GLOBALS['tpl']->assign('local',$local);
		/** 专题标签 **/
		$GLOBALS['tpl']->assign('tags',$GLOBALS['db']->getAll('select * from '.tn('tags').' where tag_type=1 group by tag_name order by Id desc'));

		/******** Search ********/
		$GLOBALS['tpl']->assign('keywords',$key);
		$psize = $GLOBALS['_param']['psize']?$GLOBALS['_param']['psize']:$GLOBALS['ora_cfg']['page_size'];
		$total = $GLOBALS['db']->count("select count(*) from ".tn('product')." where pro_status = 1 and ( instr(pro_name,'$key') or instr(pro_keywords,'$key'))");
		$pobj = new page('p',$pid,$total,$psize);
		$sql = "select op.*,oc.cat_name from  ".tn("product")." op inner join ( select Id from  ".tn("product");
		$sql .= " where pro_status = 1 and ( instr(pro_name,'$key') or instr(pro_keywords,'$key'))";
		$sql .= " order by Id desc limit ".$pobj->sqlParam.") ";
		$sql .= " b using(Id) left join ".tn("cat")." oc on op.cat_id = oc.Id";
		if($pid >= intval($pobj->maxPage/2)){
			$sql = "select op.*,oc.cat_name from  ".tn("product")." op inner join ( select Id from  ".tn("product");
			$sql .= " where pro_status = 1 and ( instr(pro_name,'$key') or instr(pro_keywords,'$key'))";
			$sql .= " order by Id asc limit ".$pobj->sqlParam.") ";
			$sql .= " b using(Id) left join ".tn("cat")." oc on op.cat_id = oc.Id";
		}
		$GLOBALS['tpl']->assign("pobj",$pobj);
		$url = get_urlStr();
		$GLOBALS['tpl']->assign('url',preg_replace('/\-?p(\-|_)\d+/i',"",$url));
		$GLOBALS['tpl']->assign('pros',$GLOBALS['db']->getAll($sql));
	}

	$GLOBALS['tpl']->display("search.html");
}


?>